#!/bin/bash


shopt -s extglob
path=`dirname $0`
version="$1"
time_info="$2"
time_node=`echo "$time_info" | awk 'BEGIN {FS="-"} {print $1 "-" $2 "-" $3 " " $4 ":" $5 ":" $6 }'`
obs_meta_start_time="2018-01-01 00:00:00"
TOOLS_dir="$version"_TOOLS
SOURCE_dir="$version"_SOURCE
repo_source_ip="10.175.107.201"
code_bin_ip="10.175.100.158"
baseline_rpm_ip="10.184.49.167"
v2r7_baseline_rpm_ip="100.109.85.100"
baseline_projects="EulerOS:2.5:Baseline:20170707 EulerOS:Baseline_20170720 EulerOS:Baseline:20170720 EulerOS:2.2:Baseline_20160729 EulerOS:V2R7:Baseline_20180608_baseline Fedora-29-1.2:Updates"
NULL="/dev/null"
LOG="`pwd`/make_tools_and_download_source.log"
script_name=`basename $0`

function log_info()
{
	echo "[`date +%Y-%m-%d-%T`]""[Info]:""[$script_name]" "$@" | tee -a $LOG
}

function log_warn()
{
	echo "[`date +%Y-%m-%d-%T`]""[Warning]:""[$script_name]" "$@" | tee -a $LOG
}

function log_error()
{
	echo "[`date +%Y-%m-%d-%T`]""[Error]:""[$script_name]" "$@" | tee -a $LOG
	exit 1
}

#获取版本缺失的工程配置文件
function get_miss_projects()
{
	log_info "check projects meta or prjconf for missing"
	local projects=$@
	local line=""
	pwd
	pushd $TOOLS_dir
	for line in $projects
	do
#获取缺失工程的.meta文件
#判断有没有这个工程的.meta文件，如果没有，从CI的工程中获取。
#如果获取失败，将退出,如果退出请检查/etc/hosts文件中CI的IP是否正确，或者判断该版本有没有这个project。
		if [ ! -e $line.meta ]
		then
			osc meta prj "$line" > "$line.meta" 2> $NULL
			if [  $? -ne 0 ]
			then
				rm "$line.meta"
				log_error "get missing project $line failed,please make sure the IP in the /etc/hosts is correct,and the version has this project"
			fi
		fi
#获取缺失的工程的.prjconf文件
		if [ ! -e $line.prjconf ]
		then
			osc meta prjconf "$line" > "$line.prjconf" 2> $NULL
			if [  $? -ne 0 ]
			then
				rm "$line.prjconf"
				log_error "get missing project $line failed,please make sure the IP in the /etc/hosts is correct,and the version has this project"
			fi
		fi
#获取缺失工程的目录
		if [ ! -e ${line//:/} ]
		then
			osc co $line 2> $NULL
		fi
	done
	popd
	log_info "Finished check, projects meta or prjconf is ok"
}

function expect_cmd()
{
	local cmd="$1"
	expect -c "
		set timeout -1
		spawn $cmd
		expect {
			\"Are you sure *\? \" {send \"yes\r\"; exp_continue}
			\"Please type *\? \" {send \"yes\r\"; exp_continue}
			\"Password:*\" {send \"EulerOS_123\r\"; exp_continue}
		}
	"
	return $?
}

#从code_bin目录中获取对应版本的配置文件
function get_projects_by_codebin()
{
	log_info "Start get projects from code_bin"
	local projects="$1"
	local line=""
	code_bin_dir=${code_bin_tar%%.tar.gz}
	for line in $projects
	do
		cp tmp/$code_bin_dir/$line.meta $TOOLS_dir
		cp tmp/$code_bin_dir/$line.prjconf $TOOLS_dir
		local dir=${line//:/}
		#判断在code_bin目录中project目录是以Euler:kernel:HCC形式，还是EulerkernelHCC形式存在的。
		if [ -e tmp/$code_bin_dir/$line ]
		then
			cp -rf "tmp/$code_bin_dir/$line" $TOOLS_dir/$dir
		elif [ -e tmp/$code_bin_dir/$dir ]
		then
			cp -rf "tmp/$code_bin_dir/$dir" $TOOLS_dir
		fi
	done
	if [ -e $TOOLS_dir/"EulerLinuxV1R1C00HVS" ] && [ `find $TOOLS_dir/"EulerLinuxV1R1C00HVS" -name bsp | wc -l ` -ne 0 ]
	then
		osc co   Euler:Linux:V1R1C00:HVS bsp
		if [ $? -ne 0 ]
		then
			log_warn "get Euler:Linux:V1R1C00:HVS bsp failed"
		fi
	fi
	log_info "Finished get projects from code_bin,successful"
}

#从obs_meta目录中获取对应版本的配置文件
function get_projects_by_obsmeta()
{
	log_info "Start get prjechs from obs_meta"
	local projects="$1"
	local line=""
	local obs_meta_dir=`find tmp -name obs_meta`
	for line in $projects
	do
		cp -rf $obs_meta_dir/projects/$line $TOOLS_dir 2> $NULL
		#如果obs_meta中没有该project的配置文件，则从code_bin中获取该工程的projects的配置文件
		if [ $? -ne 0 ]
		then
			log_warn "End get prjechs from obs_meta,failed"
			log_warn "The project "$line" don't exist in the obs_meta,try to get this projects "$projects" from code_bin."
			rm -rf $TOOLS_dir/*
			download_code_bin
			get_projects_by_codebin "$projects"
			return
		fi
		dir=${line//:/}
		mv $TOOLS_dir/$line $TOOLS_dir/$dir 2> $NULL
	done
	mv `find $TOOLS_dir -name *.meta` $TOOLS_dir
	mv `find $TOOLS_dir -name *.prjconf` $TOOLS_dir
	log_info "Finished get prjechs from obs_meta,successful"
}

function scp_baseline()
{
	local project="$1"
	local base_file="$2"
	local obj_path="$3"
	expect_cmd  "scp -r $base_file $obj_path" &> $NULL
	if [ $? -ne 0 ]
	then
		log_error "download baseline project $project of $version failed"
	fi
}

#获取基线工程rpm包
function get_baseline()
{
	log_info "Start get baseline project $1 RPM packages"
	local project=$1
	local dir=${project//:/}
	if [ -d $TOOLS_dir/$dir ]
	then
		mkdir -p $TOOLS_dir/$dir
	fi
	rm -rf $TOOLS_dir/$dir/*

	case $project in
		EulerOS:Baseline:20170720)
		local line_dir=`echo $project| sed 's/:/:\//g'`
		scp_baseline $project "root@$baseline_rpm_ip:/srv/obs/repos/$line_dir/*" "$TOOLS_dir/$dir"
		;;
		EulerOS:Baseline_20170720)
		for line in aarch64 i686 x86_64
		do
			mkdir -p $TOOLS_dir/$dir/standard_$line/$line
			scp_baseline $project "root@$baseline_rpm_ip:/srv/obs/build/$project/standard_$line/$line/:full/*.rpm" \
			"$TOOLS_dir/$dir/standard_$line/$line"
		done
		;;
		EulerOS:2.5:Baseline:20170707)
		for line in aarch64 i686 x86_64
		do
			mkdir -p $TOOLS_dir/$dir/standard/$line
			scp_baseline $project "root@$baseline_rpm_ip:/srv/obs/build/$project/standard/$line/:full/*" \
			"$TOOLS_dir/$dir/standard/$line"
		done
		;;
		EulerOS:2.2:Baseline_20160729)
		for line in i686 x86_64 
		do
			mkdir -p $TOOLS_dir/$dir/standard/$line
			scp_baseline $project "root@$baseline_rpm_ip:/srv/obs/build/$project/standard/$line/:full/*${line}.rpm" \
			"$TOOLS_dir/$dir/standard/$line"
		done
		mkdir -p $TOOLS_dir/$dir/standard/noarch
		scp_baseline $project "root@$baseline_rpm_ip:/srv/obs/build/$project/standard/i686/:full/*.noarch.rpm" \
		"$TOOLS_dir/$dir/standard/noarch"
		;;
		EulerOS:V2R7:Baseline_20180608_baseline)
		for line in aarch64 i686 x86_64
		do
			mkdir -p $TOOLS_dir/$dir/standard_$line/$line
			scp_baseline $project "root@$v2r7_baseline_rpm_ip:/srv/obs/build/$project/standard_$line/$line/:full/*.rpm" \
			"$TOOLS_dir/$dir/standard_$line/$line"
		done
		;;
		Fedora-29-1.2:Updates)
		if [ ! -d $TOOLS_dir/Fedora-29-1.2Updates ]
		then
			mkdir -p $TOOLS_dir/Fedora-29-1.2Updates
		fi
		scp -r $v2r7_baseline_rpm_ip:/srv/obs/build/Fedora-29-1.2:Updates/* 
		;;
		*)
		;;
	esac
	log_info "Finished get baseline project $project RPM packages,successful"
}

#下载源码管理方式为repo或git的版本的CI做包工具，函数共4个参数：1.CI的下载路径；2.时间戳；3.CI在SOURCE包中路径和目录名称；4.版本的分支。
function download_release_repo_by_repo()
{
	local url="$1"
	local time_node="$2"
	local repo_obj_path="$3"
	local branch="$4"
	local repo_name=`echo "$url" | awk 'BEGIN {FS="/"} {print $NF}' | sed 's/\.git//g'`

	pushd $repo_obj_path
		expect_cmd "repo sync $url"
		if [ $? -ne 0 ]
		then
			log_error "download ci of $version failed,make suse you have permission or check the url $url"
		fi
		cd $url
			repo start $branch .
			local commit=`git log --before "$time_node" -1 --format="%H"`
			if [ -z $commit ]
			then
				log_error "when download the ci of $version,git checkout time failed,please make sure the time $time_node is right!"
			fi
			git checkout $commit
		cd -
	popd
}

#下载源码管理方式为repo或git的版本的CI做包工具，函数共4个参数：1.CI的下载路径；2.时间戳；3.CI在SOURCE包中路径和目录名称；4.版本的分支。
function download_release_repo_by_git()
{
	local url="$1"
	local time_node="$2"
	local repo_obj_path="$3"
	local branch="$4"
	local repo_name=`echo "$url" | awk 'BEGIN {FS="/"} {print $NF}' | sed 's/\.git//g'`

	if [ ! -d $repo_obj_path ]
	then
		mkdir -p $repo_obj_path
	fi
	pushd $repo_obj_path
		expect_cmd "git clone \"$url\""
		if [ $? -ne 0 ]
		then
			log_error "download ci of $version failed,make suse you have permission or check the url $url"
		fi
		cd $repo_name
		git checkout $branch
		if [ $? -ne 0 ]
		then
			log_error "when download the ci of $version,git checkout branch failed,please make sure the branch $branch is right!"
		fi
		git pull
		local commit=`git log --before "$time_node" -1 --format="%H"`
		if [ -z $commit ]
		then
			log_error "when download the ci of $version,git checkout time failed,please make sure the time $time_node is right!"
		fi
		git checkout $commit
		cd -
	popd
}

#下载源码管理方式为svn的版本的CI做包工具，函数共3个参数：1.CI的下载路径；2.时间戳；3.CI在SOURCE包中路径和目录名称。
function download_release_repo_by_svn()
{
	local url="$1"
	local time_node="$2"
	local repo_obj_path="$3"
	local repo_name=`echo "$url" | awk 'BEGIN {FS="/"} {print $NF}'`

	if [ ! -d $repo_obj_path ]
	then
		mkdir -p $repo_obj_path
	fi
	pushd $repo_obj_path
		expect_cmd "svn co \"$url\""
		if [ $? -ne 0 ]
		then
			log_error "download the ci of $version failed,make suse you have permission or check the url $url"
		fi
		cd $repo_name
			svn update --force -r {$time_node}
			if [ $? -ne 0 ]
			then
				log_error "when download the ci of $version,svn update time failed,please make sure the time $time_node is right!"
			fi
		cd -
	popd
}

#下载CI做包工具
function download_ci()
{
	local base_version=$1
	local time_node="$2"
	local svn_time_info=`echo "$time_node" | awk 'BEGIN {FS="[-: ]"} {print $1 $2 $3 "T" $4 $5 $6}'`
	log_info "start download CI tools at `date`"
	#根据不同的源码管理方式，下载CI做包工具
	case $base_version in
		EulerOS_Server_V200R008C00)
		download_release_repo_by_repo "euleros/build" "$time_node"  "$SOURCE_dir/next" "next"
		;;
		EulerOS_V200R007C00)
		download_release_repo_by_repo "euleros/CI/ci_project" "$time_node"  "$SOURCE_dir/EulerOS_next_centos7.5" "next_centos7.5"
		;;
		EulerOS_V200R007C00_7.3)
		download_release_repo_by_repo "euleros/CI/ci_project" "$time_node"  "$SOURCE_dir/EulerOS_next" "next"
		;;
		EulerOS_V200R003C00_WN)
		download_release_repo_by_repo "EulerOS_V2R3C00/release_repo" "$time_node"  "$SOURCE_dir/EulerOS_V2R3C00/" "master"
		;;
		EulerOS_V200R003C00_STORAGE_X86)
		download_release_repo_by_repo "EulerOS_V2R3C00/release_repo" "$time_node"  "$SOURCE_dir/EulerOS_V2R3C00/" "master"
		;;
		EulerOS_V200R005C00)
		download_release_repo_by_repo "EulerOS/release_repo" "$time_node"  "$SOURCE_dir/EulerOS_V2R3C00_next/" "next"
		;;
		EulerOS_V100R003C00_SUSE)
		download_release_repo_by_git "git@code.huawei.com:euler/euler_os_release_repo.git" "$time_node"  "$SOURCE_dir" "master"
		;;
		EulerLinux_V100R001C10|EulerLinux_V100R001C10_MBSC)
		download_release_repo_by_svn "http://szxsvn06-rd:6801/svn/CP_RISC_SoftWare_SVN/Euler_Linux_V100R001C10/Code/trunk/ci" "$svn_time_info"  "$SOURCE_dir/EulerLinux_V100R001C10"
		;;
		DOPRA_LinuxV200R003C07)
		download_release_repo_by_svn "http://szxsvn06-rd:6801/svn/CP_RISC_SoftWare_SVN/Dopra_Linux_V2R3C07/Code/trunk/ci" "$svn_time_info"  "$SOURCE_dir/DOPRA_LinuxV200R003C07"
		;;
		*)
		log_error "the version $version error,please check version"
		;;
	esac
	log_info "Finished download CI tools at `date`"
}

#下载源码管理方式为repo或git的版本做包依赖的rpm包，函数共5个参数：1.rpm包的下载路径；2.rpm包目录列表，可能不止一个；3.时间戳；4.rpm包目录在SOURCE包中路径和目录名称；5.版本分支。
function download_baserpm_by_git()
{
	local url="$1"
	local baserpm_dirs="$2"
	local time_node="$3"
	local baserpm_obj_path="$4"
	local branch="$5"
	local download_dir_name=`echo "$url" | awk 'BEGIN {FS="/"} {print $NF}' | sed 's/\.git//g'`

	expect_cmd "git clone \"$url\""
	if [ $? -ne 0 ]
	then
		log_error "download $version $baserpm_dirs failed,make suse you have permission or check $url"
	fi
	pushd $download_dir_name
	git checkout $branch
	if [ $? -ne 0 ]
	then
		log_error "when download the baserpm of $version,git checkout branch failed,please make sure the branch $branch is right!"
	fi
	git pull
	local commit=`git log --before "$time_node" -1 --format="%H"`
	if [ -z $commit ]
	then
		log_error "when download the baserpm of $version,git checkout time failed,please make sure the time $time_node is right!"
	fi
	git checkout $commit
	popd

	for line in $baserpm_dirs
	do
		mkdir -p $baserpm_obj_path/$line
		cp -rf $download_dir_name/$line/* $baserpm_obj_path/$line
	done
	rm -rf $download_dir_name
}

#下载源码管理方式为svn的版本做包依赖的rpm包，函数共4个参数：1.rpm包的下载路径；2.rpm包目录列表，可能不止一个；3.时间戳；4.rpm包目录在SOURCE包中路径和目录名称.
function download_baserpm_by_svn()
{
	local url="$1"
	local baserpm_dirs="$2"
	local time_node="$3"
	local baserpm_obj_path="$4"
	local line=""

	for line in $baserpm_dirs
	do
		local name=`basename $line`
		local path=`dirname $line`
		expect_cmd "svn co \"$url/$line\""
		if [ $? -ne 0 ]
		then
			log_error "download $version $line failed,make suse you have permission or check $url/$line"
		fi
		pushd $name
		svn update --force -r {$time_node}
		popd
		mkdir -p "$baserpm_obj_path/$path"
		mv $name "$baserpm_obj_path/$path"
	done
}

#下载版本做包依赖的rpm包
function download_baserpm()
{
	local base_version=$1
	local time_node="$2"
	local svn_time_info=`echo "$time_node" | awk 'BEGIN {FS="[-: ]"} {print $1 $2 $3 "T" $4 $5 $6}'`
	#根据不同的源码管理方式，下载不同版本做包需要的rpm包
	log_info "Start get baserpm at `date`"
	case $base_version in
		EulerOS_V100R003C00_SUSE)
		download_baserpm_by_git "git@code.huawei.com:euler/EulerOS_V100R003C00.git" "Common_Original/suse/repos Common_Upgrade/suse/repos" "$time_node" "$SOURCE_dir/EulerOS_V100R003C00"
		;;
		EulerLinux_V100R001C10|EulerLinux_V100R001C10_MBSC)
		download_baserpm_by_svn "http://szxsvn06-rd:6801/svn/CP_RISC_SoftWare_SVN/Euler_Linux_V100R001C10/Code/trunk" "Platform_SW Common_Original/repos Common_Upgrade/repos src/tools/sys-custom-tool/sys-custom-tool-2.3.7 document/public" "$svn_time_info"  "$SOURCE_dir/EulerLinux_V100R001C10"
		;;
		DOPRA_LinuxV200R003C07)
		download_baserpm_by_svn "http://szxsvn06-rd:6801/svn/CP_RISC_SoftWare_SVN/Dopra_Linux_V2R3C07/Code/trunk" "tools" "$svn_time_info"  "$SOURCE_dir/DOPRA_LinuxV200R003C07"
		;;
		*)
		;;
	esac
	log_info "Finished get baserpm at `date`"
}

#获取EulerOS:2.5:Baseline:20170707源码编译配置文件
function get_baseline_2_5_20170707()
{
	log_info "Start get baseline project EulerOS:2.5:Baseline:20170707 configuration files"
	local flag=$1
	tar xf $v2r5_baseline -C .
	if [ $? -ne 0 ]
	then
		log_error "make sure you have $v2r5_baseline and $v2r5_baseline is right"
	fi
	cp -rf EulerOS_V2R5_baseline/{CentOS7.3*,*2.5*} $TOOLS_dir/
	if [ $? -ne 0 ]
	then
		log_error "cp EulerOS_V2R5_2_5_baseline config file failed"
	fi
	log_info "Finished get baseline project EulerOS:2.5:Baseline:20170707 configuration files,successful"
	if [ $flag -eq 1 ]
	then
		return
	else
		get_baseline_20170720
	fi
}

#获取EulerOS:Baseline_20170720源码编译配置文件
function get_baseline_20170720()
{
	log_info "Start get baseline project EulerOS:Baseline_20170720 configuration files" 
	rm -rf $TOOLS_dir/*Baseline_20170720*
	tar xf $v2r5_baseline -C .
	if [ $? -ne 0 ]
	then
		log_error "make sure you have $v2r5_baseline and $v2r5_baseline is ok"
	fi
	cp -rf EulerOS_V2R5_baseline/*Baseline_20170720* $TOOLS_dir/
	if [ $? -ne 0 ]
	then
		log_error "cp EulerOS_V2R5_20170720_baseline config file failed"
	fi
	log_info "Finished get baseline project EulerOS:Baseline_20170720 configuration files.successful" 

}

#解析配置文件，获取对应关键字的内容
function get_projects_from_conf()
{
	local key_word=$1
	local ret1=`grep -n "\[$key_word\]" $ATIA_conf | awk 'BEGIN {FS=":"} {print $1}'`
	if [ -z $ret1 ]
	then
		return
	fi
	local ret2=0
	local line=""
	for line in `grep -n "\[" $ATIA_conf | awk 'BEGIN {FS=":"} {print $1}'`
	do
		if [ $line -gt $ret1 ]
		then
			ret2=$line
			break
		fi
	done
	if [ $ret2 -eq 0 ]
	then
		ret2=`wc -l $ATIA_conf | awk "print $1"`
	fi
	ret1=`expr $ret1 + 1`
	ret2=`expr $ret2 - 1`
	sed -n ""$ret1","$ret2"p" $ATIA_conf | grep -v "#" | sed '/^$/d' | sed 's/\t//g' | sed 's/\ //g' | sed 's/\n/ /g'
}

#判断是否编译基线，并获取对应基线工程的源码或者rpm包
function build_baseline_yes_no()
{
	log_info "Start get baseline projects"
	#从配置文件中获取需要编译的基线工程
	local build_baseline_list=`get_projects_from_conf build_baseline_list`
	local line=""
	local line2=""
	local projects="$1"
	local ret1=0
	local ret2=0
	#如果版本为EulerOS_V200R005C00，在projects中增加EulerOS:Baseline:20170720
	if [ $base_version == "EulerOS_V200R005C00" ]
	then
		projects+=" EulerOS:Baseline:20170720"
	fi
	#判断配置文件中是否设置基线工程编译
	if [ -z "$build_baseline_list" ]
	then
		#如果没有设置，则判断版本projects中是否有基线工程，如果有，下载该工程的基线rpm包
		log_info "get baseline projects RPM packages"
		for line in $projects
		do
			echo "$baseline_projects" | grep $line &> $NULL
			if [ $? -eq 0 ]
			then
				get_baseline $line
			fi
		done
	else
		log_info "get baseline projects "$build_baseline_list" source code"
		for line in $build_baseline_list
		do
			#如果设置了基线工程编译，但是该工程不在本版本的工程中，则直接报错退出
			echo "$projects" | grep $line &> $NULL
			if [ $? -ne 0 ]
			then
				log_error "This version $version don't include this baseline project $line.please check config file deliver.conf"
			fi
			local flag=0
			#判断设置的是哪个基线工程需要编译，获取对应的工程配置文件和对应源码
			for line2 in $baseline_projects
			do
				echo "$projects" | grep $line2 &> $NULL
				ret1=$?
				echo "$build_baseline_list" | grep $line2 &> $NULL
				ret2=$?
				if [ $ret1 -eq 0 -a $ret2 -eq 0 ];then
						case $line2 in
						EulerOS:2.5:Baseline:20170707)
							get_baseline_2_5_20170707 $flag
							log_info "Start get baseline projects"
							return
							;;
						EulerOS:2.2:Baseline_20160729)
							get_miss_projects "EulerOS:2.1:BaseLine:ALL_Release"
							;;
						EulerOS:Baseline_20170720)
							get_baseline_20170720
							flag=1
							;;
						esac
				elif [ $ret1 -eq 0 -a $line2 == "EulerOS:2.5:Baseline:20170707" ];then
						get_baseline $line2
				fi
			done
		done
	fi
	log_info "Start get baseline projects"
}

#制作TOOLS包和SOURCE包
function mk_tools()
{
	log_info "start make TOOLS at `date`"
	mkdir -p $TOOLS_dir
	local base_version=$1
	local mk_tools_flag="$2"
	#从配置文件中解析对应版本依赖的projects
	local projects=`get_projects_from_conf $base_version`
	if [ -z "$projects" ]
	then
		log_error "This version $base_version does not have any projects in the config file deliver.conf"
	fi
	#根据不同的标签，从code_bin或者obs_meta中获取projects的配置文件,制作TOOLS包
	if [ $mk_tools_flag == "obs_meta" ]
	then
		get_projects_by_obsmeta "$projects"
	elif [ $mk_tools_flag == "code_bin" ]
	then
		get_projects_by_codebin "$projects"
	fi
	#有些工程在code_bin目录下找不到配置文件，如某些基线工程，需要从CI的工程中获取配置文件
	get_miss_projects $projects
	#获取对应版本的编译脚本,配置文件
	cp $version_build_script $TOOLS_dir
	cp $common_script $TOOLS_dir
	cp $ATIA_conf $TOOLS_dir
	sed -i '/branch/d' $TOOLS_dir/`basename $ATIA_conf`
	#判断是否需要编译基线
	build_baseline_yes_no "$projects"
	rm -rf tmp
	log_info "Finished make TOOLS at `date`"
}

#检查参数和用法
function usage()
{
	if [ $# -ne 2 ]
	then
		log_info "usage: bash mk_tools.sh version time_info"
		log_info "for example:bash mk_tools.sh EulerLinux_V100R001C10SPC321B001 2018-03-30-05-40-21"
		log_error "arg is error! please check Usage!"
	fi
	echo "$2" | grep "20[0-9]\{2\}-[0-1][0-9]-[0-3][0-9]-[0-2][0-9]-[0-6][0-9]-[0-6][0-9]" &> $NULL
	if [ $? -ne 0 ]
	then
		log_error "The argv[2] is time_info ,please check your second arg!,e.g:2018-03-30-05-40-21"
	fi
}

#下载对应时间的code_bin 备份
function download_code_bin()
{
	log_info "Start download code_bin"
	mkdir tmp
	pushd tmp
	wget http://$code_bin_ip/obs_backup/
	if [ $? -ne 0 ]
	then
		log_error "down code_bin failed, make TOOLS failed!"
	else
		local time_node_part=`echo $time_info | awk -F "-" '{printf $1 "-" $2 }'`
		local min_line=`grep -n "$time_node_part" index.html | cut -d \: -f 1 | sort -n | sed -n '1p'`
		local max_line=`grep -n "$time_node_part" index.html | cut -d \: -f 1 | sort -rn | sed -n '1p'`
		local max_line=`expr $max_line + 1`
		for line in `sed -n "${min_line},${max_line}p" index.html | cut -d \" -f 6`
		do
			local time_node1=`echo ${line%.tar.gz} | awk -F "-" '{print $1 "-" $2 "-" $3 " " $4 ":" $5 ":" $6}'`
			local time_node2=`echo $time_info | awk -F "-" '{print $1 "-" $2 "-" $3 " " $4 ":" $5 ":" $6}'`
			local t1=`date -d "$time_node1" +%s`
			local t2=`date -d "$time_node2" +%s`
			if [ $t1 -ge $t2 ]
			then
				code_bin_tar="$line"
				break
			fi
		done
		wget http://$code_bin_ip/obs_backup/$code_bin_tar
		if [ $? -ne 0 ]
		then
			log_error "download code_bin failed,make TOOLS failed!"
		fi
		tar xf $code_bin_tar
		rm index.html
	fi
	log_info "Finished download code_bin,successful"
	pushd tmp
}

#下载obs_meta,并切到对应的分支和时间点
function download_obs_meta()
{
	log_info "Start download obs_meta"
	local version="$1"
	local base_version="$2"
	local time_info="$3"

	grep "\[$base_version\]" $ATIA_conf &> $NULL
	if [ $? -ne 0 ]
	then
		log_error "The version $base_version not exist in the config file deliver.conf ,please check deliver.conf!"
	fi
	local branch=`grep ${base_version}_branch $ATIA_conf | cut -d\" -f 2`
	if [ -z $branch ]
	then
		log_error "The config file deliver.conf don't have branch of the $base_version,please check deliver.conf!"
	fi
	mkdir tmp
	pushd tmp
	case $base_version in
	EulerOS_V200R007C00_7.3)
	expect_cmd "repo init -u http://dgggit09-rd.huawei.com/CRDU_RTOS_SDK_GIT/manifest.git -b master -m EulerOS_next.xml  --no-repo-verify --repo-branch=huawei --no-clone-bundle"
	;;
	EulerOS_V200R007C00)
	expect_cmd "repo init -u http://dgggit09-rd.huawei.com/CRDU_RTOS_SDK_GIT/manifest.git -b master -m EulerOS_next_centos7.5.xml  --no-repo-verify --repo-branch=huawei --no-clone-bundle"
	;;
	EulerOS_Server_V200R008C00)
	expect_cmd "repo init -u http://dgggit09-rd.huawei.com/euleros/manifest.git -b master -m EulerOS_next.xml  --no-repo-verify --repo-branch=huawei --no-clone-bundle"
	;;
	*)
	expect_cmd "repo init -u ssh://10.175.107.201/EulerOS_V2R3C00_manifest -m default_wide.xml --repo-url=ssh://10.175.107.201/tools/repo --repo-branch huawei --no-repo-verify"
	;;
	esac
	if [ $? -eq 0 ]
	then
		repo sync obs_meta
		cd `find . -name obs_meta`
			repo start master .
			git checkout $branch
			git checkout $version 2>$NULL
			if [ $? -ne 0 ]
			then
				git checkout `git log --before "$time_info" -1 --format="%H"`
			fi
		cd -
		log_info "Finished download obs_meta,successful"
		popd
		return 0
	else
		log_warn "download obs_meta failed!"
		popd
		return 1
	fi
}

#获取EulerOS_V200R005C00版本缺失的文件
function get_V2R5_miss_files()
{
	log_info "Start get EulerOS_V200R005C00 missing files"
	local time_node="$1"
	expect_cmd "git clone \"ssh://${repo_source_ip}/euleros-latest-release\""
	if [ $? -eq 0 ]
	then
		pushd euleros-latest-release
		git checkout $version
		if [ $? -ne 0 ]
		then
			git checkout next
			git checkout `git log --before "$time_info" -1 --format="%H"`
		fi
		popd
	else
		log_warn "download euleros-latest-release of EulerOS:Custom in $version failed !"
	fi
	cp -rf euleros-latest-release $TOOLS_dir/EulerOSCustom
	if [ $? -ne 0 ]
	then
		log_warn "Copy euleros-latest-release to $SOURCE_dir failed"
	fi
	rm -rf euleros-latest-release
	log_info "Finished get EulerOS_V200R005C00 missing files,successful"
}

#检查配置文件和依赖的脚本是否存在
function check_and_clean_env()
{
	log_info "Start checking to see if the script and configuration file exist,clean environment"
	if [ -e $ATIA_conf -a -e $version_build_script -a -e $source_download_script -a -e $common_script ]
	then
		rm -rf !($v2r5_baseline||$mk_tools_script|$source_download_script|build_scripts)
	else
		log_error "$ATIA_conf or $version_build_script or $common_script or $source_download_script does not exist ,please check!"
	fi
	rm -rf ../../../../../.repo/
	log_info "Finished check file, clean environment successful"
}

function main()
{
	#检查参数并指出脚本用法
	usage $@
	#获取基础版本，如：EulerOS_V200R003C00_WN
	base_version=`echo "$version" | sed 's/SPC[0-9]\{3\}B[0-9]\{1,3\}//g' | sed 's/B[0-9]\{1,3\}//g'`
	if [ $base_version == EulerOS_V200R007C00 ]
	then
		release=`echo "$version" | sed 's/^.*SPC//g' | sed 's/B[0-9]\{1,3\}//g'`
		if [ $release -lt 300 ]
		then
			base_version=`echo $base_version | sed 's/$/_7.3/g'`
		fi
	fi
	local local_path=`pwd`
	ATIA_conf="$local_path/build_scripts/$base_version/deliver.conf"
	version_build_script="$local_path/build_scripts/$base_version/`echo $base_version | tr "A-Z" "a-z"`.sh"
	common_script="$local_path/build_scripts/common.sh"
	source_download_script="$local_path/source_download.py"
	mk_tools_script="$local_path/mk_tools.sh"
	v2r5_baseline="$local_path/baseline/EulerOS_V2R5_baseline.tar.gz"
	#清理环境
	check_and_clean_env
	local mk_tools_flag=""
	#对比时间，判断是下载obs_meta还是code_bin.
	local t1=`date -d "$time_node" +%s`
	local t2=`date -d "$obs_meta_start_time" +%s`
	#如果版本时间在2018-01-01 00:00:00 之前下载code_bin，如果在之后下载obs_meta
	if [ $t1 -le $t2 ]
	then
		download_code_bin
		mk_tools_flag="code_bin"
	else
		download_obs_meta "$version" "$base_version" "$time_info"
		if [ $? -ne 0 ]
		then
			log_info "download obs_meta failed,try to get projects config file from code_bin"
			#如果下载obs_meta失败，则下载code_bin
			download_code_bin
			mk_tools_flag="code_bin"
		else
			mk_tools_flag="obs_meta"
		fi
	fi
	#制作TOOLS包
	mk_tools "$base_version" "$mk_tools_flag"
	log_info "Start download SOURCE at `date`"
	#下载源码
	expect_cmd "python $source_download_script -d $TOOLS_dir -t \"$time_node\" " | egrep "success|failure|finished" | tee -a $LOG
	if [ $? -ne 0 ]
	then
		log_warn "some package download source failed,please check this package"
	fi
	rm -rf tmp
	#获取EulerOS_V200R005C00版本缺失的配置文件和源码
	if [ $base_version == "EulerOS_V200R005C00" ]
	then
		get_V2R5_miss_files "$time_node"
	fi
	log_info "Finished download SOURCE at `date`"
	#根据版本获取相应的ci工具
	download_ci $base_version "$time_node"
	#获取版本做包需要的rpm包
	download_baserpm $base_version "$time_node"
	#打包TOOLS包和SOURCE包
	tar czf ${TOOLS_dir}.tar.gz ${TOOLS_dir}
	rm -rf ${TOOLS_dir}
	tar czf ${SOURCE_dir}.tar.gz ${SOURCE_dir}
	rm -rf ${SOURCE_dir}
}
pushd $path
main $@
popd
mv $path/${TOOLS_dir}.tar.gz .
mv $path/${SOURCE_dir}.tar.gz .
log_info "Finished make Tools and download SOURCE at `date`"
